<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-pragma">{-# LINE 1 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-1"></span><span class="hs-pragma">{-# LANGUAGE BangPatterns #-}</span><span>
</span><span id="line-2"></span><span class="hs-pragma">{-# LANGUAGE CPP #-}</span><span>
</span><span id="line-3"></span><span class="hs-pragma">{-# LANGUAGE DoAndIfThenElse #-}</span><span>
</span><span id="line-4"></span><span class="hs-pragma">{-# LANGUAGE ForeignFunctionInterface #-}</span><span>
</span><span id="line-5"></span><span class="hs-pragma">{-# LANGUAGE EmptyDataDecls #-}</span><span>
</span><span id="line-6"></span><span class="hs-pragma">{-# LANGUAGE RecordWildCards #-}</span><span>
</span><span id="line-7"></span><span class="hs-pragma">{-# LANGUAGE NoImplicitPrelude #-}</span><span>
</span><span id="line-8"></span><span>
</span><span id="line-9"></span><span class="hs-comment">-------------------------------------------------------------------------------</span><span>
</span><span id="line-10"></span><span class="hs-comment">-- |</span><span>
</span><span id="line-11"></span><span class="hs-comment">-- Module      :  GHC.Event.Windows.FFI</span><span>
</span><span id="line-12"></span><span class="hs-comment">-- Copyright   :  (c) Tamar Christina 2019</span><span>
</span><span id="line-13"></span><span class="hs-comment">-- License     :  BSD-style (see the file libraries/base/LICENSE)</span><span>
</span><span id="line-14"></span><span class="hs-comment">--</span><span>
</span><span id="line-15"></span><span class="hs-comment">-- Maintainer  :  libraries@haskell.org</span><span>
</span><span id="line-16"></span><span class="hs-comment">-- Stability   :  experimental</span><span>
</span><span id="line-17"></span><span class="hs-comment">-- Portability :  non-portable</span><span>
</span><span id="line-18"></span><span class="hs-comment">--</span><span>
</span><span id="line-19"></span><span class="hs-comment">-- WinIO Windows API Foreign Function imports</span><span>
</span><span id="line-20"></span><span class="hs-comment">--</span><span>
</span><span id="line-21"></span><span class="hs-comment">-------------------------------------------------------------------------------</span><span>
</span><span id="line-22"></span><span>
</span><span id="line-23"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">GHC.Event.Windows.FFI</span><span> </span><span class="hs-special">(</span><span>
</span><span id="line-24"></span><span>    </span><span class="hs-comment">-- * IOCP</span><span>
</span><span id="line-25"></span><span>    </span><span class="hs-identifier">IOCP</span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span class="hs-special">,</span><span>
</span><span id="line-26"></span><span>    </span><span class="hs-identifier">CompletionKey</span><span class="hs-special">,</span><span>
</span><span id="line-27"></span><span>    </span><span class="hs-identifier">newIOCP</span><span class="hs-special">,</span><span>
</span><span id="line-28"></span><span>    </span><span class="hs-identifier">associateHandleWithIOCP</span><span class="hs-special">,</span><span>
</span><span id="line-29"></span><span>    </span><span class="hs-identifier">getQueuedCompletionStatusEx</span><span class="hs-special">,</span><span>
</span><span id="line-30"></span><span>    </span><span class="hs-identifier">postQueuedCompletionStatus</span><span class="hs-special">,</span><span>
</span><span id="line-31"></span><span>    </span><span class="hs-identifier">getOverlappedResult</span><span class="hs-special">,</span><span>
</span><span id="line-32"></span><span>
</span><span id="line-33"></span><span>    </span><span class="hs-comment">-- * Completion Data</span><span>
</span><span id="line-34"></span><span>    </span><span class="hs-identifier">CompletionData</span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span class="hs-special">,</span><span>
</span><span id="line-35"></span><span>    </span><span class="hs-identifier">CompletionCallback</span><span class="hs-special">,</span><span>
</span><span id="line-36"></span><span>    </span><span class="hs-identifier">withRequest</span><span class="hs-special">,</span><span>
</span><span id="line-37"></span><span>
</span><span id="line-38"></span><span>    </span><span class="hs-comment">-- * Overlapped</span><span>
</span><span id="line-39"></span><span>    </span><span class="hs-identifier">OVERLAPPED</span><span class="hs-special">,</span><span>
</span><span id="line-40"></span><span>    </span><span class="hs-identifier">LPOVERLAPPED</span><span class="hs-special">,</span><span>
</span><span id="line-41"></span><span>    </span><span class="hs-identifier">OVERLAPPED_ENTRY</span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span class="hs-special">,</span><span>
</span><span id="line-42"></span><span>    </span><span class="hs-identifier">LPOVERLAPPED_ENTRY</span><span class="hs-special">,</span><span>
</span><span id="line-43"></span><span>    </span><span class="hs-identifier">HASKELL_OVERLAPPED</span><span class="hs-special">,</span><span>
</span><span id="line-44"></span><span>    </span><span class="hs-identifier">LPHASKELL_OVERLAPPED</span><span class="hs-special">,</span><span>
</span><span id="line-45"></span><span>    </span><span class="hs-identifier">allocOverlapped</span><span class="hs-special">,</span><span>
</span><span id="line-46"></span><span>    </span><span class="hs-identifier">zeroOverlapped</span><span class="hs-special">,</span><span>
</span><span id="line-47"></span><span>    </span><span class="hs-identifier">pokeOffsetOverlapped</span><span class="hs-special">,</span><span>
</span><span id="line-48"></span><span>    </span><span class="hs-identifier">overlappedIOStatus</span><span class="hs-special">,</span><span>
</span><span id="line-49"></span><span>    </span><span class="hs-identifier">overlappedIONumBytes</span><span class="hs-special">,</span><span>
</span><span id="line-50"></span><span>
</span><span id="line-51"></span><span>    </span><span class="hs-comment">-- * Cancel pending I/O</span><span>
</span><span id="line-52"></span><span>    </span><span class="hs-identifier">cancelIoEx</span><span class="hs-special">,</span><span>
</span><span id="line-53"></span><span>    </span><span class="hs-identifier">cancelIoEx'</span><span class="hs-special">,</span><span>
</span><span id="line-54"></span><span>
</span><span id="line-55"></span><span>    </span><span class="hs-comment">-- * Monotonic time</span><span>
</span><span id="line-56"></span><span>
</span><span id="line-57"></span><span>    </span><span class="hs-comment">-- ** GetTickCount</span><span>
</span><span id="line-58"></span><span>    </span><span class="hs-identifier">getTickCount64</span><span class="hs-special">,</span><span>
</span><span id="line-59"></span><span>
</span><span id="line-60"></span><span>    </span><span class="hs-comment">-- ** QueryPerformanceCounter</span><span>
</span><span id="line-61"></span><span>    </span><span class="hs-identifier">queryPerformanceCounter</span><span class="hs-special">,</span><span>
</span><span id="line-62"></span><span>    </span><span class="hs-identifier">queryPerformanceFrequency</span><span class="hs-special">,</span><span>
</span><span id="line-63"></span><span>
</span><span id="line-64"></span><span>    </span><span class="hs-comment">-- ** Miscellaneous</span><span>
</span><span id="line-65"></span><span>    </span><span class="hs-identifier">throwWinErr</span><span class="hs-special">,</span><span>
</span><span id="line-66"></span><span>    </span><span class="hs-identifier">setLastError</span><span>
</span><span id="line-67"></span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span class="hs-cpp">





#include &quot;windows_cconv.h&quot;
</span><span>
</span><span id="line-75"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">Data.Maybe</span><span>
</span><span id="line-76"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">Foreign</span><span>
</span><span id="line-77"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.Base</span><span>
</span><span id="line-78"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.Num</span><span> </span><span class="hs-special">(</span><span class="hs-special">(</span><span class="hs-operator">*</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-79"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.Real</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">fromIntegral</span><span class="hs-special">)</span><span>
</span><span id="line-80"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.Show</span><span>
</span><span id="line-81"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.Windows</span><span>
</span><span id="line-82"></span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">qualified</span><span> </span><span class="hs-identifier">GHC.Event.Array</span><span> </span><span class="hs-keyword">as</span><span> </span><span class="hs-identifier">A</span><span>
</span><span id="line-83"></span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">qualified</span><span> </span><span class="hs-identifier">GHC.Windows</span><span>     </span><span class="hs-keyword">as</span><span> </span><span class="hs-identifier">Win32</span><span>
</span><span id="line-84"></span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">GHC.IO.Handle.Internals</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">debugIO</span><span class="hs-special">)</span><span>
</span><span id="line-85"></span><span>
</span><span id="line-86"></span><span class="hs-comment">------------------------------------------------------------------------</span><span>
</span><span id="line-87"></span><span class="hs-comment">-- IOCP</span><span>
</span><span id="line-88"></span><span>
</span><span id="line-89"></span><span class="hs-comment">-- | An I/O completion port.</span><span>
</span><span id="line-90"></span><span class="hs-keyword">newtype</span><span> </span><span class="hs-identifier">IOCP</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">IOCP</span><span> </span><span class="hs-identifier">HANDLE</span><span>
</span><span id="line-91"></span><span>    </span><span class="hs-keyword">deriving</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Eq</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">Ord</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">Show</span><span class="hs-special">)</span><span>
</span><span id="line-92"></span><span>
</span><span id="line-93"></span><span class="hs-keyword">type</span><span> </span><span class="hs-identifier">CompletionKey</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">ULONG_PTR</span><span>
</span><span id="line-94"></span><span>
</span><span id="line-95"></span><span class="hs-comment">-- | This function has two distinct purposes depending on the value of</span><span>
</span><span id="line-96"></span><span class="hs-comment">-- The completion port handle:</span><span>
</span><span id="line-97"></span><span class="hs-comment">--</span><span>
</span><span id="line-98"></span><span class="hs-comment">--  - When the IOCP port is NULL then the function creates a new I/O completion</span><span>
</span><span id="line-99"></span><span class="hs-comment">--    port.  See `newIOCP`.</span><span>
</span><span id="line-100"></span><span class="hs-comment">--</span><span>
</span><span id="line-101"></span><span class="hs-comment">--  - When The port contains a valid handle then the given handle is</span><span>
</span><span id="line-102"></span><span class="hs-comment">--    associated with he given completion port handle.  Once associated it</span><span>
</span><span id="line-103"></span><span class="hs-comment">--    cannot be easily changed.  Associating a Handle with a Completion Port</span><span>
</span><span id="line-104"></span><span class="hs-comment">--    allows the I/O manager's worker threads to handle requests to the given</span><span>
</span><span id="line-105"></span><span class="hs-comment">--    handle.</span><span>
</span><span id="line-106"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-keyword">unsafe</span><span> </span><span class="hs-string">&quot;windows.h CreateIoCompletionPort&quot;</span><span>
</span><span id="line-107"></span><span>    </span><span class="hs-identifier">c_CreateIoCompletionPort</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IOCP</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">ULONG_PTR</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">DWORD</span><span>
</span><span id="line-108"></span><span>                             </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">IOCP</span><span>
</span><span id="line-109"></span><span>
</span><span id="line-110"></span><span class="hs-comment">-- | Create a new I/O completion port.</span><span>
</span><span id="line-111"></span><span class="hs-identifier">newIOCP</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">IOCP</span><span>
</span><span id="line-112"></span><span class="hs-identifier">newIOCP</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">failIf</span><span> </span><span class="hs-special">(</span><span class="hs-operator">==</span><span> </span><span class="hs-identifier">IOCP</span><span> </span><span class="hs-identifier">nullPtr</span><span class="hs-special">)</span><span> </span><span class="hs-string">&quot;newIOCP&quot;</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-113"></span><span>          </span><span class="hs-identifier">c_CreateIoCompletionPort</span><span> </span><span class="hs-identifier">iNVALID_HANDLE_VALUE</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">IOCP</span><span> </span><span class="hs-identifier">nullPtr</span><span class="hs-special">)</span><span> </span><span class="hs-number">0</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-114"></span><span>
</span><span id="line-115"></span><span class="hs-comment">-- | Associate a HANDLE with an I/O completion port.</span><span>
</span><span id="line-116"></span><span class="hs-identifier">associateHandleWithIOCP</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IOCP</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">CompletionKey</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-117"></span><span class="hs-identifier">associateHandleWithIOCP</span><span> </span><span class="hs-identifier">iocp</span><span> </span><span class="hs-identifier">handle</span><span> </span><span class="hs-identifier">completionKey</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-118"></span><span>    </span><span class="hs-identifier">failIf_</span><span> </span><span class="hs-special">(</span><span class="hs-operator">/=</span><span> </span><span class="hs-identifier">iocp</span><span class="hs-special">)</span><span> </span><span class="hs-string">&quot;associateHandleWithIOCP&quot;</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-119"></span><span>        </span><span class="hs-identifier">c_CreateIoCompletionPort</span><span> </span><span class="hs-identifier">handle</span><span> </span><span class="hs-identifier">iocp</span><span> </span><span class="hs-identifier">completionKey</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-120"></span><span>
</span><span id="line-121"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;windows.h GetOverlappedResult&quot;</span><span>
</span><span id="line-122"></span><span>    </span><span class="hs-identifier">c_GetOverlappedResult</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">LPOVERLAPPED</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">DWORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-123"></span><span>                          </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-124"></span><span>
</span><span id="line-125"></span><span class="hs-comment">-- | Get the result of a single overlap operation without the IO manager</span><span>
</span><span id="line-126"></span><span class="hs-identifier">getOverlappedResult</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">OVERLAPPED</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">BOOL</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Maybe</span><span> </span><span class="hs-identifier">DWORD</span><span class="hs-special">)</span><span>
</span><span id="line-127"></span><span class="hs-identifier">getOverlappedResult</span><span> </span><span class="hs-identifier">handle</span><span> </span><span class="hs-identifier">lp</span><span> </span><span class="hs-identifier">block</span><span>
</span><span id="line-128"></span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">alloca</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">bytes</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-129"></span><span>        </span><span class="hs-keyword">do</span><span> </span><span class="hs-identifier">res</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">c_GetOverlappedResult</span><span> </span><span class="hs-identifier">handle</span><span> </span><span class="hs-identifier">lp</span><span> </span><span class="hs-identifier">bytes</span><span> </span><span class="hs-identifier">block</span><span>
</span><span id="line-130"></span><span>           </span><span class="hs-keyword">if</span><span> </span><span class="hs-identifier">res</span><span>
</span><span id="line-131"></span><span>              </span><span class="hs-keyword">then</span><span> </span><span class="hs-identifier">fmap</span><span> </span><span class="hs-identifier">Just</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">peek</span><span> </span><span class="hs-identifier">bytes</span><span>
</span><span id="line-132"></span><span>              </span><span class="hs-keyword">else</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">Nothing</span><span>
</span><span id="line-133"></span><span>
</span><span id="line-134"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-keyword">safe</span><span> </span><span class="hs-string">&quot;windows.h GetQueuedCompletionStatusEx&quot;</span><span>
</span><span id="line-135"></span><span>    </span><span class="hs-identifier">c_GetQueuedCompletionStatusEx</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IOCP</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">LPOVERLAPPED_ENTRY</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Word32</span><span>
</span><span id="line-136"></span><span>                                  </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">ULONG</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">DWORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">BOOL</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-137"></span><span>
</span><span id="line-138"></span><span class="hs-comment">-- | Note [Completion Ports]</span><span>
</span><span id="line-139"></span><span class="hs-comment">-- When an I/O operation has been queued by an operation</span><span>
</span><span id="line-140"></span><span class="hs-comment">-- (ReadFile/WriteFile/etc) it is placed in a queue that the driver uses when</span><span>
</span><span id="line-141"></span><span class="hs-comment">-- servicing IRQs.  This queue has some important properties:</span><span>
</span><span id="line-142"></span><span class="hs-comment">--</span><span>
</span><span id="line-143"></span><span class="hs-comment">-- 1.) It is not an ordered queue.  Requests may be performed out of order as</span><span>
</span><span id="line-144"></span><span class="hs-comment">--     as the OS's native I/O manager may try to re-order requests such that as</span><span>
</span><span id="line-145"></span><span class="hs-comment">--     few random seeks as possible are needed to complete the pending</span><span>
</span><span id="line-146"></span><span class="hs-comment">--     operations.  As such do not assume a fixed order between something being</span><span>
</span><span id="line-147"></span><span class="hs-comment">--     queued and dequeued.</span><span>
</span><span id="line-148"></span><span class="hs-comment">--</span><span>
</span><span id="line-149"></span><span class="hs-comment">-- 2.) Operations may skip the queue entirely.  In which case they do not end in</span><span>
</span><span id="line-150"></span><span class="hs-comment">--     in this function. (This is an optimization flag we have turned on. See</span><span>
</span><span id="line-151"></span><span class="hs-comment">--     `openFile`.)</span><span>
</span><span id="line-152"></span><span class="hs-comment">--</span><span>
</span><span id="line-153"></span><span class="hs-comment">-- 3.) Across this call the specified OVERLAPPED_ENTRY buffer MUST remain live,</span><span>
</span><span id="line-154"></span><span class="hs-comment">--     and the buffer for an I/O operation cannot be freed or moved until</span><span>
</span><span id="line-155"></span><span class="hs-comment">--     `getOverlappedResult` says it's done.  The reason is the kernel may not</span><span>
</span><span id="line-156"></span><span class="hs-comment">--     have fully released the buffer, or finished writing to it when this</span><span>
</span><span id="line-157"></span><span class="hs-comment">--     operation returns.  Failure to adhere to this will cause your IRQs to be</span><span>
</span><span id="line-158"></span><span class="hs-comment">--     silently dropped and your program will never receive a completion for it.</span><span>
</span><span id="line-159"></span><span class="hs-comment">--     This means that the OVERLAPPED buffer must also remain valid for the</span><span>
</span><span id="line-160"></span><span class="hs-comment">--     duration of the call and as such must be allocated on the unmanaged heap.</span><span>
</span><span id="line-161"></span><span class="hs-comment">--</span><span>
</span><span id="line-162"></span><span class="hs-comment">-- 4.) When a thread calls this method it is associated with the I/O manager's</span><span>
</span><span id="line-163"></span><span class="hs-comment">--     worker threads pool.  You should always use dedicated threads for this</span><span>
</span><span id="line-164"></span><span class="hs-comment">--     since the OS I/O manager will now monitor the threads.  If the thread</span><span>
</span><span id="line-165"></span><span class="hs-comment">--     becomes blocked for whatever reason, the Haskell I/O manager will wake up</span><span>
</span><span id="line-166"></span><span class="hs-comment">--     another threads from it's pool to service the remaining results.</span><span>
</span><span id="line-167"></span><span class="hs-comment">--     A new thread will also be woken up from the pool when the previous thread</span><span>
</span><span id="line-168"></span><span class="hs-comment">--     is busy servicing requests and new requests have finished.  For this</span><span>
</span><span id="line-169"></span><span class="hs-comment">--     reason the Haskell I/O manager multiplexes I/O operations from N haskell</span><span>
</span><span id="line-170"></span><span class="hs-comment">--     threads into 1 completion port, which is serviced by M native threads in</span><span>
</span><span id="line-171"></span><span class="hs-comment">--     an asynchronous method. This allows it to scale efficiently.</span><span>
</span><span id="line-172"></span><span class="hs-identifier">getQueuedCompletionStatusEx</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IOCP</span><span>
</span><span id="line-173"></span><span>                            </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">A.Array</span><span> </span><span class="hs-identifier">OVERLAPPED_ENTRY</span><span>
</span><span id="line-174"></span><span>                            </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">DWORD</span><span>  </span><span class="hs-comment">-- ^ Timeout in milliseconds (or</span><span>
</span><span id="line-175"></span><span>                                      </span><span class="hs-comment">-- 'GHC.Windows.iNFINITE')</span><span>
</span><span id="line-176"></span><span>                            </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Int</span><span>
</span><span id="line-177"></span><span class="hs-identifier">getQueuedCompletionStatusEx</span><span> </span><span class="hs-identifier">iocp</span><span> </span><span class="hs-identifier">arr</span><span> </span><span class="hs-identifier">timeout</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-178"></span><span>    </span><span class="hs-identifier">alloca</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">num_removed_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span class="hs-keyword">do</span><span>
</span><span id="line-179"></span><span>        </span><span class="hs-identifier">A.unsafeLoad</span><span> </span><span class="hs-identifier">arr</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">oes</span><span> </span><span class="hs-identifier">cap</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-180"></span><span>            </span><span class="hs-comment">-- TODO: remove after debugging</span><span>
</span><span id="line-181"></span><span>            </span><span class="hs-identifier">fillBytes</span><span> </span><span class="hs-identifier">oes</span><span> </span><span class="hs-number">0</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">cap</span><span> </span><span class="hs-operator">*</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">sizeOf</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">undefined</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">OVERLAPPED_ENTRY</span><span class="hs-special">)</span><span class="hs-special">)</span><span class="hs-special">)</span><span>
</span><span id="line-182"></span><span>            </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-string">&quot;-- call getQueuedCompletionStatusEx &quot;</span><span>
</span><span id="line-183"></span><span>            </span><span class="hs-comment">-- don't block the call if the rts is not supporting threads.</span><span>
</span><span id="line-184"></span><span>            </span><span class="hs-comment">-- this would block the entire program.</span><span>
</span><span id="line-185"></span><span>            </span><span class="hs-keyword">let</span><span> </span><span class="hs-identifier">alertable</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">False</span><span> </span><span class="hs-comment">-- not rtsSupportsBoundThreads</span><span>
</span><span id="line-186"></span><span>            </span><span class="hs-identifier">ok</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">c_GetQueuedCompletionStatusEx</span><span> </span><span class="hs-identifier">iocp</span><span> </span><span class="hs-identifier">oes</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-identifier">cap</span><span class="hs-special">)</span><span>
</span><span id="line-187"></span><span>                  </span><span class="hs-identifier">num_removed_ptr</span><span> </span><span class="hs-identifier">timeout</span><span> </span><span class="hs-identifier">alertable</span><span>
</span><span id="line-188"></span><span>            </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-string">&quot;-- call getQueuedCompletionStatusEx: &quot;</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-identifier">ok</span><span>
</span><span id="line-189"></span><span>            </span><span class="hs-identifier">err</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">getLastError</span><span>
</span><span id="line-190"></span><span>            </span><span class="hs-identifier">nc</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">peek</span><span> </span><span class="hs-identifier">num_removed_ptr</span><span class="hs-special">)</span><span>
</span><span id="line-191"></span><span>            </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-string">&quot;-- getQueuedCompletionStatusEx: n=&quot;</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-identifier">nc</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-string">&quot; ,err=&quot;</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-identifier">err</span><span>
</span><span id="line-192"></span><span>            </span><span class="hs-keyword">if</span><span> </span><span class="hs-identifier">ok</span><span> </span><span class="hs-keyword">then</span><span> </span><span class="hs-identifier">fromIntegral</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">fmap</span><span class="hs-special">`</span><span> </span><span class="hs-identifier">peek</span><span> </span><span class="hs-identifier">num_removed_ptr</span><span>
</span><span id="line-193"></span><span>            </span><span class="hs-keyword">else</span><span> </span><span class="hs-keyword">do</span><span> </span><span class="hs-identifier">debugIO</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-string">&quot;failed getQueuedCompletionStatusEx: &quot;</span><span> </span><span class="hs-operator">++</span><span> </span><span class="hs-identifier">show</span><span> </span><span class="hs-identifier">err</span><span>
</span><span id="line-194"></span><span>                    </span><span class="hs-keyword">if</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-operator">==</span><span> </span><span class="hs-number">258</span><span> </span><span class="hs-operator">||</span><span> </span><span class="hs-identifier">alertable</span><span> </span><span class="hs-keyword">then</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-number">0</span><span>
</span><span id="line-195"></span><span class="hs-pragma">{-# LINE 195 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-195"></span><span>                    </span><span class="hs-keyword">else</span><span> </span><span class="hs-identifier">failWith</span><span> </span><span class="hs-string">&quot;GetQueuedCompletionStatusEx&quot;</span><span> </span><span class="hs-identifier">err</span><span>
</span><span id="line-196"></span><span>
</span><span id="line-197"></span><span class="hs-identifier">overlappedIOStatus</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">LPOVERLAPPED</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">NTSTATUS</span><span>
</span><span id="line-198"></span><span class="hs-identifier">overlappedIOStatus</span><span> </span><span class="hs-identifier">lpol</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-199"></span><span>  </span><span class="hs-identifier">status</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">peekByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">0</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">lpol</span><span>
</span><span id="line-200"></span><span class="hs-pragma">{-# LINE 200 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-200"></span><span>  </span><span class="hs-comment">-- TODO: Map NTSTATUS to ErrCode?</span><span>
</span><span id="line-201"></span><span>  </span><span class="hs-comment">-- See https://github.com/libuv/libuv/blob/b12624c13693c4d29ca84b3556eadc9e9c0936a4/src/win/winsock.c#L153</span><span>
</span><span id="line-202"></span><span>  </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">status</span><span>
</span><span id="line-203"></span><span class="hs-pragma">{-# INLINE</span><span> </span><span class="hs-pragma">overlappedIOStatus</span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-204"></span><span>
</span><span id="line-205"></span><span class="hs-identifier">overlappedIONumBytes</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">LPOVERLAPPED</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">ULONG_PTR</span><span>
</span><span id="line-206"></span><span class="hs-identifier">overlappedIONumBytes</span><span> </span><span class="hs-identifier">lpol</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-207"></span><span>  </span><span class="hs-identifier">bytes</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">peekByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">8</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">lpol</span><span>
</span><span id="line-208"></span><span class="hs-pragma">{-# LINE 208 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-208"></span><span>  </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">bytes</span><span>
</span><span id="line-209"></span><span class="hs-pragma">{-# INLINE</span><span> </span><span class="hs-pragma">overlappedIONumBytes</span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-210"></span><span>
</span><span id="line-211"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-keyword">unsafe</span><span> </span><span class="hs-string">&quot;windows.h PostQueuedCompletionStatus&quot;</span><span>
</span><span id="line-212"></span><span>    </span><span class="hs-identifier">c_PostQueuedCompletionStatus</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IOCP</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">DWORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">ULONG_PTR</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">LPOVERLAPPED</span><span>
</span><span id="line-213"></span><span>                                 </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-214"></span><span>
</span><span id="line-215"></span><span class="hs-comment">-- | Manually post a completion to the specified I/O port.  This will wake up</span><span>
</span><span id="line-216"></span><span class="hs-comment">-- a thread waiting `GetQueuedCompletionStatusEx`.</span><span>
</span><span id="line-217"></span><span class="hs-identifier">postQueuedCompletionStatus</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IOCP</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">DWORD</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">CompletionKey</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">LPOVERLAPPED</span><span>
</span><span id="line-218"></span><span>                           </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-219"></span><span class="hs-identifier">postQueuedCompletionStatus</span><span> </span><span class="hs-identifier">iocp</span><span> </span><span class="hs-identifier">numBytes</span><span> </span><span class="hs-identifier">completionKey</span><span> </span><span class="hs-identifier">lpol</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-220"></span><span>    </span><span class="hs-identifier">failIfFalse_</span><span> </span><span class="hs-string">&quot;PostQueuedCompletionStatus&quot;</span><span> </span><span class="hs-operator">$</span><span>
</span><span id="line-221"></span><span>    </span><span class="hs-identifier">c_PostQueuedCompletionStatus</span><span> </span><span class="hs-identifier">iocp</span><span> </span><span class="hs-identifier">numBytes</span><span> </span><span class="hs-identifier">completionKey</span><span> </span><span class="hs-identifier">lpol</span><span>
</span><span id="line-222"></span><span>
</span><span id="line-223"></span><span class="hs-comment">------------------------------------------------------------------------</span><span>
</span><span id="line-224"></span><span class="hs-comment">-- Completion Data</span><span>
</span><span id="line-225"></span><span>
</span><span id="line-226"></span><span class="hs-comment">-- | Called when the completion is delivered.</span><span>
</span><span id="line-227"></span><span class="hs-keyword">type</span><span> </span><span class="hs-identifier">CompletionCallback</span><span> </span><span class="hs-identifier">a</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">ErrCode</span><span>   </span><span class="hs-comment">-- ^ 0 indicates success</span><span>
</span><span id="line-228"></span><span>                          </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">DWORD</span><span>     </span><span class="hs-comment">-- ^ Number of bytes transferred</span><span>
</span><span id="line-229"></span><span>                          </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">a</span><span>
</span><span id="line-230"></span><span>
</span><span id="line-231"></span><span class="hs-comment">-- | Callback type that will be called when an I/O operation completes.</span><span>
</span><span id="line-232"></span><span class="hs-keyword">type</span><span> </span><span class="hs-identifier">IOCallback</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">CompletionCallback</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-233"></span><span>
</span><span id="line-234"></span><span class="hs-comment">-- | Wrap the IOCallback type into a FunPtr.</span><span>
</span><span id="line-235"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-string">&quot;wrapper&quot;</span><span>
</span><span id="line-236"></span><span>  </span><span class="hs-identifier">wrapIOCallback</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IOCallback</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">FunPtr</span><span> </span><span class="hs-identifier">IOCallback</span><span class="hs-special">)</span><span>
</span><span id="line-237"></span><span>
</span><span id="line-238"></span><span class="hs-comment">-- | Unwrap a FunPtr IOCallback to a normal Haskell function.</span><span>
</span><span id="line-239"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-keyword">ccall</span><span> </span><span class="hs-string">&quot;dynamic&quot;</span><span>
</span><span id="line-240"></span><span>  </span><span class="hs-identifier">mkIOCallback</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">FunPtr</span><span> </span><span class="hs-identifier">IOCallback</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IOCallback</span><span>
</span><span id="line-241"></span><span>
</span><span id="line-242"></span><span class="hs-comment">-- | Structure that the I/O manager uses to associate callbacks with</span><span>
</span><span id="line-243"></span><span class="hs-comment">-- additional payload such as their OVERLAPPED structure and Win32 handle</span><span>
</span><span id="line-244"></span><span class="hs-comment">-- etc.  *Must* be kept in sync with that in `winio_structs.h` or horrible things</span><span>
</span><span id="line-245"></span><span class="hs-comment">-- happen.</span><span>
</span><span id="line-246"></span><span class="hs-comment">--</span><span>
</span><span id="line-247"></span><span class="hs-comment">-- We keep the handle around for the benefit of ghc-external libraries making</span><span>
</span><span id="line-248"></span><span class="hs-comment">-- use of the manager.</span><span>
</span><span id="line-249"></span><span class="hs-keyword">data</span><span> </span><span class="hs-identifier">CompletionData</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">CompletionData</span><span> </span><span class="hs-special">{</span><span> </span><span class="hs-identifier">cdHandle</span><span>   </span><span class="hs-glyph">::</span><span> </span><span class="hs-glyph">!</span><span class="hs-identifier">HANDLE</span><span>
</span><span id="line-250"></span><span>                                     </span><span class="hs-special">,</span><span> </span><span class="hs-identifier">cdCallback</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-glyph">!</span><span class="hs-identifier">IOCallback</span><span>
</span><span id="line-251"></span><span>                                     </span><span class="hs-special">}</span><span>
</span><span id="line-252"></span><span>
</span><span id="line-253"></span><span class="hs-keyword">instance</span><span> </span><span class="hs-identifier">Storable</span><span> </span><span class="hs-identifier">CompletionData</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-254"></span><span>    </span><span class="hs-identifier">sizeOf</span><span> </span><span class="hs-identifier">_</span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="hs-special">(</span><span class="hs-number">16</span><span class="hs-special">)</span><span>
</span><span id="line-255"></span><span class="hs-pragma">{-# LINE 255 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-255"></span><span>    </span><span class="hs-identifier">alignment</span><span> </span><span class="hs-identifier">_</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-number">8</span><span>
</span><span id="line-256"></span><span class="hs-pragma">{-# LINE 256 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-256"></span><span>
</span><span id="line-257"></span><span>    </span><span class="hs-identifier">peek</span><span> </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-258"></span><span>      </span><span class="hs-identifier">cdCallback</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">mkIOCallback</span><span> </span><span class="hs-special">`</span><span class="hs-identifier">fmap</span><span class="hs-special">`</span><span> </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">peekByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">8</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">ptr</span><span>
</span><span id="line-259"></span><span class="hs-pragma">{-# LINE 259 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-259"></span><span>      </span><span class="hs-identifier">cdHandle</span><span>   </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">peekByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">0</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">ptr</span><span>
</span><span id="line-260"></span><span class="hs-pragma">{-# LINE 260 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-260"></span><span>      </span><span class="hs-keyword">let</span><span> </span><span class="hs-glyph">!</span><span class="hs-identifier">cd</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">CompletionData</span><span class="hs-special">{</span><span class="hs-glyph">..</span><span class="hs-special">}</span><span>
</span><span id="line-261"></span><span>      </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">cd</span><span>
</span><span id="line-262"></span><span>
</span><span id="line-263"></span><span>    </span><span class="hs-identifier">poke</span><span> </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-identifier">CompletionData</span><span class="hs-special">{</span><span class="hs-glyph">..</span><span class="hs-special">}</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-264"></span><span>      </span><span class="hs-identifier">cb</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">wrapIOCallback</span><span> </span><span class="hs-identifier">cdCallback</span><span>
</span><span id="line-265"></span><span>      </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">pokeByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">8</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-identifier">cb</span><span>
</span><span id="line-266"></span><span class="hs-pragma">{-# LINE 266 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-266"></span><span>      </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">pokeByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">0</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-identifier">cdHandle</span><span>
</span><span id="line-267"></span><span class="hs-pragma">{-# LINE 267 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-267"></span><span>
</span><span id="line-268"></span><span class="hs-comment">------------------------------------------------------------------------</span><span>
</span><span id="line-269"></span><span class="hs-comment">-- Overlapped</span><span>
</span><span id="line-270"></span><span>
</span><span id="line-271"></span><span class="hs-comment">-- | Tag type for @LPOVERLAPPED@.</span><span>
</span><span id="line-272"></span><span class="hs-keyword">data</span><span> </span><span class="hs-identifier">OVERLAPPED</span><span>
</span><span id="line-273"></span><span>
</span><span id="line-274"></span><span class="hs-comment">-- | Tag type for the extended version of @OVERLAPPED@ containg some book</span><span>
</span><span id="line-275"></span><span class="hs-comment">--   keeping information.</span><span>
</span><span id="line-276"></span><span class="hs-keyword">data</span><span> </span><span class="hs-identifier">HASKELL_OVERLAPPED</span><span>
</span><span id="line-277"></span><span>
</span><span id="line-278"></span><span class="hs-comment">-- | Identifies an I/O operation.  Used as the @LPOVERLAPPED@ parameter</span><span>
</span><span id="line-279"></span><span class="hs-comment">-- for overlapped I/O functions (e.g. @ReadFile@, @WSASend@).</span><span>
</span><span id="line-280"></span><span class="hs-keyword">type</span><span> </span><span class="hs-identifier">LPOVERLAPPED</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">OVERLAPPED</span><span>
</span><span id="line-281"></span><span>
</span><span id="line-282"></span><span class="hs-comment">-- | Pointer to the extended HASKELL_OVERLAPPED function.</span><span>
</span><span id="line-283"></span><span class="hs-keyword">type</span><span> </span><span class="hs-identifier">LPHASKELL_OVERLAPPED</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">HASKELL_OVERLAPPED</span><span>
</span><span id="line-284"></span><span>
</span><span id="line-285"></span><span class="hs-comment">-- | An array of these is passed to GetQueuedCompletionStatusEx as an output</span><span>
</span><span id="line-286"></span><span class="hs-comment">-- argument.</span><span>
</span><span id="line-287"></span><span class="hs-keyword">data</span><span> </span><span class="hs-identifier">OVERLAPPED_ENTRY</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">OVERLAPPED_ENTRY</span><span> </span><span class="hs-special">{</span><span>
</span><span id="line-288"></span><span>      </span><span class="hs-identifier">lpCompletionKey</span><span>            </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">ULONG_PTR</span><span class="hs-special">,</span><span>
</span><span id="line-289"></span><span>      </span><span class="hs-identifier">lpOverlapped</span><span>               </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">LPOVERLAPPED</span><span class="hs-special">,</span><span>
</span><span id="line-290"></span><span>      </span><span class="hs-identifier">dwNumberOfBytesTransferred</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">DWORD</span><span>
</span><span id="line-291"></span><span>    </span><span class="hs-special">}</span><span>
</span><span id="line-292"></span><span>
</span><span id="line-293"></span><span class="hs-keyword">type</span><span> </span><span class="hs-identifier">LPOVERLAPPED_ENTRY</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">OVERLAPPED_ENTRY</span><span>
</span><span id="line-294"></span><span>
</span><span id="line-295"></span><span class="hs-keyword">instance</span><span> </span><span class="hs-identifier">Storable</span><span> </span><span class="hs-identifier">OVERLAPPED_ENTRY</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-296"></span><span>    </span><span class="hs-identifier">sizeOf</span><span> </span><span class="hs-identifier">_</span><span>    </span><span class="hs-glyph">=</span><span> </span><span class="hs-special">(</span><span class="hs-number">32</span><span class="hs-special">)</span><span>
</span><span id="line-297"></span><span class="hs-pragma">{-# LINE 297 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-297"></span><span>    </span><span class="hs-identifier">alignment</span><span> </span><span class="hs-identifier">_</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-number">8</span><span>
</span><span id="line-298"></span><span class="hs-pragma">{-# LINE 298 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-298"></span><span>
</span><span id="line-299"></span><span>    </span><span class="hs-identifier">peek</span><span> </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-300"></span><span>      </span><span class="hs-identifier">lpCompletionKey</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">peekByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">0</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">ptr</span><span>
</span><span id="line-301"></span><span class="hs-pragma">{-# LINE 301 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-301"></span><span>      </span><span class="hs-identifier">lpOverlapped</span><span>    </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">peekByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">8</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">ptr</span><span>
</span><span id="line-302"></span><span class="hs-pragma">{-# LINE 302 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-302"></span><span>      </span><span class="hs-identifier">dwNumberOfBytesTransferred</span><span> </span><span class="hs-glyph">&lt;-</span><span>
</span><span id="line-303"></span><span>          </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">peekByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">24</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">ptr</span><span>
</span><span id="line-304"></span><span class="hs-pragma">{-# LINE 304 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-304"></span><span>      </span><span class="hs-keyword">let</span><span> </span><span class="hs-glyph">!</span><span class="hs-identifier">oe</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">OVERLAPPED_ENTRY</span><span class="hs-special">{</span><span class="hs-glyph">..</span><span class="hs-special">}</span><span>
</span><span id="line-305"></span><span>      </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">oe</span><span>
</span><span id="line-306"></span><span>
</span><span id="line-307"></span><span>    </span><span class="hs-identifier">poke</span><span> </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-identifier">OVERLAPPED_ENTRY</span><span class="hs-special">{</span><span class="hs-glyph">..</span><span class="hs-special">}</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-308"></span><span>      </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">pokeByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">0</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-identifier">lpCompletionKey</span><span>
</span><span id="line-309"></span><span class="hs-pragma">{-# LINE 309 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-309"></span><span>      </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">pokeByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">8</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-identifier">lpOverlapped</span><span>
</span><span id="line-310"></span><span class="hs-pragma">{-# LINE 310 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-310"></span><span>      </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">pokeByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">24</span><span class="hs-special">)</span><span>
</span><span id="line-311"></span><span class="hs-pragma">{-# LINE 311 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-311"></span><span>        </span><span class="hs-identifier">ptr</span><span> </span><span class="hs-identifier">dwNumberOfBytesTransferred</span><span>
</span><span id="line-312"></span><span>
</span><span id="line-313"></span><span class="hs-comment">-- | Allocate a new</span><span>
</span><span id="line-314"></span><span class="hs-comment">-- &lt;http://msdn.microsoft.com/en-us/library/windows/desktop/ms684342%28v=vs.85%29.aspx</span><span>
</span><span id="line-315"></span><span class="hs-comment">-- OVERLAPPED&gt; structure on the unmanaged heap. This also zeros the memory to</span><span>
</span><span id="line-316"></span><span class="hs-comment">-- prevent the values inside the struct to be incorrectlt interpreted as data</span><span>
</span><span id="line-317"></span><span class="hs-comment">-- payload.</span><span>
</span><span id="line-318"></span><span class="hs-comment">--</span><span>
</span><span id="line-319"></span><span class="hs-comment">-- We extend the overlapped structure with some extra book keeping information</span><span>
</span><span id="line-320"></span><span class="hs-comment">-- such that we don't have to do a lookup on the Haskell side.</span><span>
</span><span id="line-321"></span><span class="hs-comment">--</span><span>
</span><span id="line-322"></span><span class="hs-comment">-- Future: We can gain some performance here by using a pool instead of calling</span><span>
</span><span id="line-323"></span><span class="hs-comment">--         malloc for each request. A simple block allocator would be very</span><span>
</span><span id="line-324"></span><span class="hs-comment">--         useful here, especially when we implement sockets support.</span><span>
</span><span id="line-325"></span><span class="hs-identifier">allocOverlapped</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Word64</span><span> </span><span class="hs-comment">-- ^ Offset/OffsetHigh</span><span>
</span><span id="line-326"></span><span>                </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">HASKELL_OVERLAPPED</span><span class="hs-special">)</span><span>
</span><span id="line-327"></span><span class="hs-identifier">allocOverlapped</span><span> </span><span class="hs-identifier">offset</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-328"></span><span>  </span><span class="hs-identifier">lpol</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">mallocBytes</span><span> </span><span class="hs-special">(</span><span class="hs-number">40</span><span class="hs-special">)</span><span>
</span><span id="line-329"></span><span class="hs-pragma">{-# LINE 329 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-329"></span><span>  </span><span class="hs-identifier">zeroOverlapped</span><span> </span><span class="hs-identifier">lpol</span><span>
</span><span id="line-330"></span><span>  </span><span class="hs-identifier">pokeOffsetOverlapped</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">castPtr</span><span> </span><span class="hs-identifier">lpol</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">offset</span><span>
</span><span id="line-331"></span><span>  </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">lpol</span><span>
</span><span id="line-332"></span><span>
</span><span id="line-333"></span><span class="hs-comment">-- | Zero-fill an HASKELL_OVERLAPPED structure.</span><span>
</span><span id="line-334"></span><span class="hs-identifier">zeroOverlapped</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">LPHASKELL_OVERLAPPED</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-335"></span><span class="hs-identifier">zeroOverlapped</span><span> </span><span class="hs-identifier">lpol</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">fillBytes</span><span> </span><span class="hs-identifier">lpol</span><span> </span><span class="hs-number">0</span><span> </span><span class="hs-special">(</span><span class="hs-number">40</span><span class="hs-special">)</span><span>
</span><span id="line-336"></span><span class="hs-pragma">{-# LINE 336 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-336"></span><span class="hs-pragma">{-# INLINE</span><span> </span><span class="hs-pragma">zeroOverlapped</span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-337"></span><span>
</span><span id="line-338"></span><span class="hs-comment">-- | Set the offset field in an OVERLAPPED structure.</span><span>
</span><span id="line-339"></span><span class="hs-identifier">pokeOffsetOverlapped</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">LPOVERLAPPED</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Word64</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-340"></span><span class="hs-identifier">pokeOffsetOverlapped</span><span> </span><span class="hs-identifier">lpol</span><span> </span><span class="hs-identifier">offset</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-341"></span><span>  </span><span class="hs-keyword">let</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">offsetHigh</span><span class="hs-special">,</span><span> </span><span class="hs-identifier">offsetLow</span><span class="hs-special">)</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Win32.ddwordToDwords</span><span> </span><span class="hs-identifier">offset</span><span>
</span><span id="line-342"></span><span>  </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">pokeByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">16</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">lpol</span><span> </span><span class="hs-identifier">offsetLow</span><span>
</span><span id="line-343"></span><span class="hs-pragma">{-# LINE 343 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-343"></span><span>  </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">pokeByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">20</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">lpol</span><span> </span><span class="hs-identifier">offsetHigh</span><span>
</span><span id="line-344"></span><span class="hs-pragma">{-# LINE 344 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-344"></span><span class="hs-pragma">{-# INLINE</span><span> </span><span class="hs-pragma">pokeOffsetOverlapped</span><span> </span><span class="hs-pragma">#-}</span><span>
</span><span id="line-345"></span><span>
</span><span id="line-346"></span><span class="hs-comment">------------------------------------------------------------------------</span><span>
</span><span id="line-347"></span><span class="hs-comment">-- Request management</span><span>
</span><span id="line-348"></span><span>
</span><span id="line-349"></span><span class="hs-identifier">withRequest</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">Word64</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">CompletionData</span><span>
</span><span id="line-350"></span><span>            </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">HASKELL_OVERLAPPED</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">CompletionData</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">a</span><span class="hs-special">)</span><span>
</span><span id="line-351"></span><span>            </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">a</span><span>
</span><span id="line-352"></span><span class="hs-identifier">withRequest</span><span> </span><span class="hs-identifier">offset</span><span> </span><span class="hs-identifier">cbData</span><span> </span><span class="hs-identifier">f</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-353"></span><span>    </span><span class="hs-comment">-- Create the completion record and store it.</span><span>
</span><span id="line-354"></span><span>    </span><span class="hs-comment">-- We only need the record when we enqueue a request, however if we</span><span>
</span><span id="line-355"></span><span>    </span><span class="hs-comment">-- delay creating it then we will run into a race condition where the</span><span>
</span><span id="line-356"></span><span>    </span><span class="hs-comment">-- driver may have finished servicing the request before we were ready</span><span>
</span><span id="line-357"></span><span>    </span><span class="hs-comment">-- and so the request won't have the book keeping information to know</span><span>
</span><span id="line-358"></span><span>    </span><span class="hs-comment">-- what to do.  So because of that we always create the payload,  If we</span><span>
</span><span id="line-359"></span><span>    </span><span class="hs-comment">-- need it ok, if we don't that's no problem.  This approach prevents</span><span>
</span><span id="line-360"></span><span>    </span><span class="hs-comment">-- expensive lookups in hash-tables.</span><span>
</span><span id="line-361"></span><span>    </span><span class="hs-comment">--</span><span>
</span><span id="line-362"></span><span>    </span><span class="hs-comment">-- Todo: Use a memory pool for this so we don't have to hit malloc every</span><span>
</span><span id="line-363"></span><span>    </span><span class="hs-comment">--       time.  This would allow us to scale better.</span><span>
</span><span id="line-364"></span><span>    </span><span class="hs-identifier">allocaBytes</span><span> </span><span class="hs-special">(</span><span class="hs-number">40</span><span class="hs-special">)</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">hs_lpol</span><span> </span><span class="hs-glyph">-&gt;</span><span>
</span><span id="line-365"></span><span class="hs-pragma">{-# LINE 365 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-365"></span><span>      </span><span class="hs-identifier">with</span><span> </span><span class="hs-identifier">cbData</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">cdData</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-366"></span><span>        </span><span class="hs-identifier">zeroOverlapped</span><span> </span><span class="hs-identifier">hs_lpol</span><span>
</span><span id="line-367"></span><span>        </span><span class="hs-identifier">pokeOffsetOverlapped</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">castPtr</span><span> </span><span class="hs-identifier">hs_lpol</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">offset</span><span>
</span><span id="line-368"></span><span>        </span><span class="hs-identifier">f</span><span> </span><span class="hs-identifier">hs_lpol</span><span> </span><span class="hs-identifier">cdData</span><span>
</span><span id="line-369"></span><span>
</span><span id="line-370"></span><span class="hs-comment">------------------------------------------------------------------------</span><span>
</span><span id="line-371"></span><span class="hs-comment">-- Cancel pending I/O</span><span>
</span><span id="line-372"></span><span>
</span><span id="line-373"></span><span class="hs-comment">-- | CancelIo shouldn't block, but cancellation happens infrequently,</span><span>
</span><span id="line-374"></span><span class="hs-comment">-- so we might as well be on the safe side.</span><span>
</span><span id="line-375"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-keyword">unsafe</span><span> </span><span class="hs-string">&quot;windows.h CancelIoEx&quot;</span><span>
</span><span id="line-376"></span><span>    </span><span class="hs-identifier">c_CancelIoEx</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">LPOVERLAPPED</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-377"></span><span>
</span><span id="line-378"></span><span class="hs-comment">-- | Cancel all pending overlapped I/O for the given file that was initiated by</span><span>
</span><span id="line-379"></span><span class="hs-comment">-- the current OS thread.  Cancelling is just a request for cancellation and</span><span>
</span><span id="line-380"></span><span class="hs-comment">-- before the OVERLAPPED struct is freed we must make sure that the IRQ has been</span><span>
</span><span id="line-381"></span><span class="hs-comment">-- removed from the queue.  See `getOverlappedResult`.</span><span>
</span><span id="line-382"></span><span class="hs-identifier">cancelIoEx</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">LPOVERLAPPED</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-383"></span><span class="hs-identifier">cancelIoEx</span><span> </span><span class="hs-identifier">h</span><span> </span><span class="hs-identifier">o</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">failIfFalse_</span><span> </span><span class="hs-string">&quot;CancelIoEx&quot;</span><span> </span><span class="hs-operator">.</span><span> </span><span class="hs-identifier">c_CancelIoEx</span><span> </span><span class="hs-identifier">h</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-identifier">o</span><span>
</span><span id="line-384"></span><span>
</span><span id="line-385"></span><span class="hs-identifier">cancelIoEx'</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">HANDLE</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">LPOVERLAPPED</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Bool</span><span>
</span><span id="line-386"></span><span class="hs-identifier">cancelIoEx'</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">c_CancelIoEx</span><span>
</span><span id="line-387"></span><span>
</span><span id="line-388"></span><span class="hs-comment">------------------------------------------------------------------------</span><span>
</span><span id="line-389"></span><span class="hs-comment">-- Monotonic time</span><span>
</span><span id="line-390"></span><span>
</span><span id="line-391"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-string">&quot;windows.h GetTickCount64&quot;</span><span>
</span><span id="line-392"></span><span>    </span><span class="hs-identifier">c_GetTickCount64</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Word64</span><span>
</span><span id="line-393"></span><span class="hs-pragma">{-# LINE 393 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-393"></span><span>
</span><span id="line-394"></span><span class="hs-comment">-- | Call the @GetTickCount64@ function, which returns a monotonic time in</span><span>
</span><span id="line-395"></span><span class="hs-comment">-- milliseconds.</span><span>
</span><span id="line-396"></span><span class="hs-comment">--</span><span>
</span><span id="line-397"></span><span class="hs-comment">-- Problems:</span><span>
</span><span id="line-398"></span><span class="hs-comment">--</span><span>
</span><span id="line-399"></span><span class="hs-comment">--  * Low resolution (10 to 16 milliseconds).</span><span>
</span><span id="line-400"></span><span class="hs-comment">--</span><span>
</span><span id="line-401"></span><span class="hs-comment">-- &lt;http://msdn.microsoft.com/en-us/library/windows/desktop/ms724408%28v=vs.85%29.aspx&gt;</span><span>
</span><span id="line-402"></span><span class="hs-identifier">getTickCount64</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Word64</span><span>
</span><span id="line-403"></span><span class="hs-identifier">getTickCount64</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">c_GetTickCount64</span><span>
</span><span id="line-404"></span><span>
</span><span id="line-405"></span><span class="hs-comment">-- | Call the @QueryPerformanceCounter@ function.</span><span>
</span><span id="line-406"></span><span class="hs-comment">--</span><span>
</span><span id="line-407"></span><span class="hs-comment">-- Problems:</span><span>
</span><span id="line-408"></span><span class="hs-comment">--</span><span>
</span><span id="line-409"></span><span class="hs-comment">--  * Might not be available on some hardware.  Use 'queryPerformanceFrequency'</span><span>
</span><span id="line-410"></span><span class="hs-comment">--    to test for availability before calling this function.</span><span>
</span><span id="line-411"></span><span class="hs-comment">--</span><span>
</span><span id="line-412"></span><span class="hs-comment">--  * On a multiprocessor computer, may produce different results on</span><span>
</span><span id="line-413"></span><span class="hs-comment">--    different processors due to hardware bugs.</span><span>
</span><span id="line-414"></span><span class="hs-comment">--</span><span>
</span><span id="line-415"></span><span class="hs-comment">-- To get a monotonic time in seconds, divide the result of</span><span>
</span><span id="line-416"></span><span class="hs-comment">-- 'queryPerformanceCounter' by that of 'queryPerformanceFrequency'.</span><span>
</span><span id="line-417"></span><span class="hs-comment">--</span><span>
</span><span id="line-418"></span><span class="hs-comment">-- &lt;http://msdn.microsoft.com/en-us/library/windows/desktop/ms644904%28v=vs.85%29.aspx&gt;</span><span>
</span><span id="line-419"></span><span class="hs-identifier">queryPerformanceCounter</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">Int64</span><span>
</span><span id="line-420"></span><span class="hs-identifier">queryPerformanceCounter</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-421"></span><span>    </span><span class="hs-identifier">callQP</span><span> </span><span class="hs-identifier">c_QueryPerformanceCounter</span><span>
</span><span id="line-422"></span><span>    </span><span class="hs-operator">&gt;&gt;=</span><span> </span><span class="hs-identifier">maybe</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">throwGetLastError</span><span> </span><span class="hs-string">&quot;QueryPerformanceCounter&quot;</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">return</span><span>
</span><span id="line-423"></span><span>
</span><span id="line-424"></span><span class="hs-comment">-- | Call the @QueryPerformanceFrequency@ function.  Return 'Nothing' if the</span><span>
</span><span id="line-425"></span><span class="hs-comment">-- hardware does not provide a high-resolution performance counter.</span><span>
</span><span id="line-426"></span><span class="hs-comment">--</span><span>
</span><span id="line-427"></span><span class="hs-comment">-- &lt;http://msdn.microsoft.com/en-us/library/windows/desktop/ms644905%28v=vs.85%29.aspx&gt;</span><span>
</span><span id="line-428"></span><span class="hs-identifier">queryPerformanceFrequency</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Maybe</span><span> </span><span class="hs-identifier">Int64</span><span class="hs-special">)</span><span>
</span><span id="line-429"></span><span class="hs-identifier">queryPerformanceFrequency</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-430"></span><span>    </span><span class="hs-identifier">m</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">callQP</span><span> </span><span class="hs-identifier">c_QueryPerformanceFrequency</span><span>
</span><span id="line-431"></span><span>    </span><span class="hs-keyword">case</span><span> </span><span class="hs-identifier">m</span><span> </span><span class="hs-keyword">of</span><span>
</span><span id="line-432"></span><span>        </span><span class="hs-identifier">Nothing</span><span>   </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">Nothing</span><span>
</span><span id="line-433"></span><span>        </span><span class="hs-identifier">Just</span><span> </span><span class="hs-number">0</span><span>    </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">Nothing</span><span> </span><span class="hs-comment">-- Shouldn't happen; just a safeguard to</span><span>
</span><span id="line-434"></span><span>                                    </span><span class="hs-comment">-- avoid a zero denominator.</span><span>
</span><span id="line-435"></span><span>        </span><span class="hs-identifier">Just</span><span> </span><span class="hs-identifier">freq</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Just</span><span> </span><span class="hs-identifier">freq</span><span class="hs-special">)</span><span>
</span><span id="line-436"></span><span>
</span><span id="line-437"></span><span class="hs-keyword">type</span><span> </span><span class="hs-identifier">QPFunc</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Ptr</span><span> </span><span class="hs-identifier">Int64</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">BOOL</span><span>
</span><span id="line-438"></span><span>
</span><span id="line-439"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-string">&quot;Windows.h QueryPerformanceCounter&quot;</span><span>
</span><span id="line-440"></span><span>    </span><span class="hs-identifier">c_QueryPerformanceCounter</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">QPFunc</span><span>
</span><span id="line-441"></span><span>
</span><span id="line-442"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-string">&quot;Windows.h QueryPerformanceFrequency&quot;</span><span>
</span><span id="line-443"></span><span>    </span><span class="hs-identifier">c_QueryPerformanceFrequency</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">QPFunc</span><span>
</span><span id="line-444"></span><span>
</span><span id="line-445"></span><span class="hs-identifier">callQP</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">QPFunc</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Maybe</span><span> </span><span class="hs-identifier">Int64</span><span class="hs-special">)</span><span>
</span><span id="line-446"></span><span class="hs-identifier">callQP</span><span> </span><span class="hs-identifier">qpfunc</span><span> </span><span class="hs-glyph">=</span><span>
</span><span id="line-447"></span><span>    </span><span class="hs-identifier">allocaBytes</span><span> </span><span class="hs-special">(</span><span class="hs-number">8</span><span class="hs-special">)</span><span> </span><span class="hs-operator">$</span><span> </span><span class="hs-glyph">\</span><span class="hs-identifier">ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-448"></span><span class="hs-pragma">{-# LINE 448 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-448"></span><span>        </span><span class="hs-identifier">ok</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-identifier">qpfunc</span><span> </span><span class="hs-identifier">ptr</span><span>
</span><span id="line-449"></span><span>        </span><span class="hs-keyword">if</span><span> </span><span class="hs-identifier">ok</span><span> </span><span class="hs-keyword">then</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-450"></span><span>            </span><span class="hs-identifier">n</span><span> </span><span class="hs-glyph">&lt;-</span><span> </span><span class="hs-special">(</span><span class="hs-glyph">\</span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">peekByteOff</span><span> </span><span class="hs-identifier">hsc_ptr</span><span> </span><span class="hs-number">0</span><span class="hs-special">)</span><span> </span><span class="hs-identifier">ptr</span><span>
</span><span id="line-451"></span><span class="hs-pragma">{-# LINE 451 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-451"></span><span>            </span><span class="hs-identifier">return</span><span> </span><span class="hs-special">(</span><span class="hs-identifier">Just</span><span> </span><span class="hs-identifier">n</span><span class="hs-special">)</span><span>
</span><span id="line-452"></span><span>        </span><span class="hs-keyword">else</span><span>
</span><span id="line-453"></span><span>            </span><span class="hs-identifier">return</span><span> </span><span class="hs-identifier">Nothing</span><span>
</span><span id="line-454"></span><span>
</span><span id="line-455"></span><span class="hs-comment">------------------------------------------------------------------------</span><span>
</span><span id="line-456"></span><span class="hs-comment">-- Miscellaneous</span><span>
</span><span id="line-457"></span><span>
</span><span id="line-458"></span><span class="hs-keyword">type</span><span> </span><span class="hs-identifier">ULONG_PTR</span><span>  </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">Word64</span><span>
</span><span id="line-459"></span><span class="hs-pragma">{-# LINE 459 &quot;libraries\\base\\GHC\\Event\\Windows\\FFI.hsc&quot; #-}</span><span>
</span><span id="line-459"></span><span>
</span><span id="line-460"></span><span class="hs-identifier">throwWinErr</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">String</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">ErrCode</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-identifier">a</span><span>
</span><span id="line-461"></span><span class="hs-identifier">throwWinErr</span><span> </span><span class="hs-identifier">loc</span><span> </span><span class="hs-identifier">err</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-keyword">do</span><span>
</span><span id="line-462"></span><span>    </span><span class="hs-identifier">c_SetLastError</span><span> </span><span class="hs-identifier">err</span><span>
</span><span id="line-463"></span><span>    </span><span class="hs-identifier">Win32.failWith</span><span> </span><span class="hs-identifier">loc</span><span> </span><span class="hs-identifier">err</span><span>
</span><span id="line-464"></span><span>
</span><span id="line-465"></span><span class="hs-identifier">setLastError</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">ErrCode</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-466"></span><span class="hs-identifier">setLastError</span><span> </span><span class="hs-glyph">=</span><span> </span><span class="hs-identifier">c_SetLastError</span><span>
</span><span id="line-467"></span><span>
</span><span id="line-468"></span><span class="hs-keyword">foreign</span><span> </span><span class="hs-keyword">import</span><span> </span><span class="hs-identifier">WINDOWS_CCONV</span><span> </span><span class="hs-keyword">unsafe</span><span> </span><span class="hs-string">&quot;windows.h SetLastError&quot;</span><span>
</span><span id="line-469"></span><span>    </span><span class="hs-identifier">c_SetLastError</span><span> </span><span class="hs-glyph">::</span><span> </span><span class="hs-identifier">ErrCode</span><span> </span><span class="hs-glyph">-&gt;</span><span> </span><span class="hs-identifier">IO</span><span> </span><span class="hs-special">(</span><span class="hs-special">)</span><span>
</span><span id="line-470"></span></pre></body></html>